class _LazyMan{
    constructor(name) {
        this.tasks = [];
        const task = () => {
            console.log(`Hi! This is ${name}`);
            this.next();
        };
        this.tasks.push(task);
        setTimeout(() => {
            this.next();
        }, 0);
    };

    next() {
        const task = this.tasks.shift(); // 取出第一个任务
        task && task();
    };

    sleep(time) {
        this._sleepWrapper(time, false);
        return this;
    };

    sleepFirst(time) {
        this._sleepWrapper(time, true);
        return this;
    };

    _sleepWrapper(time,first) {
        const task = () => {
            setTimeout(() => {
                console.log(`Wake up after ${time}`);
            }, time * 1000);
        };

        if (first) {
            this.tasks.unshift(task);
        } else {
            this.tasks.push(task);
        };
    };

    
    eat(type) {
        const task = () => {
            console.log(`Eat ${type}`);
            this.next();
        };
        this.tasks.push(task);
        return this;
    };
};


function LazyMan(name) {
    return new _LazyMan(name);
};